package com.huawei.hwid.api.common;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import com.huawei.cloudservice.b;
import com.huawei.hms.support.api.entity.sns.SNSCode.Status;
import com.huawei.hwid.core.d.b.e;
import com.huawei.hwid.core.helper.handler.ErrorStatus;
import java.util.Queue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

public final class a implements ServiceConnection {
    private static final AtomicInteger a = new AtomicInteger();
    private static final ExecutorService b = Executors.newFixedThreadPool(3);
    private static a c;
    private final AtomicBoolean d = new AtomicBoolean(false);
    private final Queue<b> e = new LinkedBlockingQueue();
    private AtomicInteger f = new AtomicInteger(0);
    private CountDownLatch g = null;
    private b h;
    private Context i;
    private int j = 0;
    private Handler k = new Handler(this, Looper.getMainLooper()) {
        final /* synthetic */ a a;

        public void handleMessage(Message message) {
            super.handleMessage(message);
            if (message.what == Status.HWID_UNLOGIN) {
                e.b("AIDLClientManager", "msg.what=MSG_ADD_TASK", true);
                if (this.a.f.get() == 2) {
                    this.a.c();
                } else if (this.a.f.get() == 0) {
                    this.a.d();
                }
            } else if (message.what == 3002) {
                this.a.f.set(2);
                this.a.c();
            } else if (message.what == 3003) {
                this.a.f.set(0);
                this.a.j();
            } else if (message.what == 3004) {
                e.b("AIDLClientManager", "MSG_FINISH_TASK, unbind", true);
                this.a.i();
                this.a.f.set(0);
            } else if (message.what == Status.GET_USER_DATA_FAIL) {
                e.b("AIDLClientManager", "MSG_START_SERVICE_FAILED all login return failed", true);
                this.a.f.set(0);
                this.a.h();
            }
        }
    };

    private a(Context context) {
        this.i = context.getApplicationContext();
    }

    public static a a(Context context) {
        if (context == null) {
            return null;
        }
        a aVar;
        synchronized (a.class) {
            if (c == null) {
                e.b("AIDLClientManager", "AIDLClientManager init", true);
                c = new a(context);
            }
            aVar = c;
        }
        return aVar;
    }

    public final b a() {
        return this.h;
    }

    public void b() {
        e.b("AIDLClientManager", "sendTaskFinishMsg", true);
        synchronized (this.e) {
            if (a.decrementAndGet() == 0) {
                this.k.sendEmptyMessage(3004);
            }
        }
    }

    public void a(b bVar) {
        e.b("AIDLClientManager", "addTask:", true);
        synchronized (this.e) {
            a.incrementAndGet();
            this.e.add(bVar);
        }
        this.k.sendEmptyMessage(Status.HWID_UNLOGIN);
    }

    private void c() {
        e.b("AIDLClientManager", "doTask", true);
        synchronized (this.e) {
            while (true) {
                b bVar = (b) this.e.poll();
                if (bVar != null) {
                    try {
                        b.submit(bVar);
                    } catch (RejectedExecutionException e) {
                        e.d("AIDLClientManager", "Execute submit RejectedExecutionException!", true);
                    } catch (NullPointerException e2) {
                        e.d("AIDLClientManager", "Execute submit NullPointerException!", true);
                    } catch (Exception e3) {
                        e.d("AIDLClientManager", "Execute submit Exception!", true);
                    }
                }
            }
        }
    }

    private void d() {
        e.b("AIDLClientManager", "startService", true);
        this.f.set(1);
        e();
        f();
    }

    private void e() {
        e.a("AIDLClientManager", "begin to bindService", true);
        Intent intent = new Intent();
        intent.setAction("com.huawei.hwid.ICloudService");
        intent.setPackage("com.huawei.hwid");
        try {
            if (!this.i.bindService(intent, this, 1)) {
                e.d("AIDLClientManager", "bind service failed", true);
            }
        } catch (Exception e) {
            e.b("AIDLClientManager", "bind service exception", true);
        }
    }

    private void f() {
        this.d.set(false);
        this.g = new CountDownLatch(1);
        new Thread(new Runnable(this) {
            final /* synthetic */ a a;

            {
                this.a = r1;
            }

            public void run() {
                try {
                    boolean await = this.a.g.await(2000, TimeUnit.MILLISECONDS);
                    e.b("AIDLClientManager", "startService await", true);
                    if (!await) {
                        this.a.g();
                    }
                } catch (InterruptedException e) {
                    e.b("AIDLClientManager", "serviceStartDownLatch InterruptedException", true);
                    this.a.g();
                }
            }
        }).start();
    }

    private void g() {
        e.b("AIDLClientManager", "onServiceConnectedTimeout", true);
        if (!this.d.get()) {
            i();
            if (this.j < 3) {
                this.j++;
                d();
                return;
            }
            this.d.set(true);
            this.k.sendEmptyMessage(Status.GET_USER_DATA_FAIL);
        }
    }

    private void h() {
        e.b("AIDLClientManager", "onBindServiceFailed", true);
        synchronized (this.e) {
            while (true) {
                b bVar = (b) this.e.poll();
                if (bVar != null) {
                    a.decrementAndGet();
                    bVar.a(new ErrorStatus(40, "start APK service ERROR"));
                }
            }
        }
    }

    private void i() {
        e.b("AIDLClientManager", "unbind Service", true);
        try {
            this.i.unbindService(this);
        } catch (Exception e) {
            e.b("AIDLClientManager", "unbind service error", true);
        }
        j();
    }

    private void j() {
        synchronized (this.e) {
            this.h = null;
        }
    }

    public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
        e.b("AIDLClientManager", "onServiceConnected", true);
        try {
            this.j = 0;
            this.h = com.huawei.cloudservice.b.a.a(iBinder);
            this.g.countDown();
            this.d.set(true);
            this.k.sendEmptyMessage(3002);
        } catch (RuntimeException e) {
            e.d("AIDLClientManager", "onServiceConnected RuntimeException!", true);
        } catch (Exception e2) {
            e.d("AIDLClientManager", "onServiceConnected Exception!", true);
        }
    }

    public void onServiceDisconnected(ComponentName componentName) {
        e.b("AIDLClientManager", "onServiceDisconnected", true);
        this.j = 0;
        this.k.sendEmptyMessage(3003);
    }
}
